﻿//Copyright (c) 2009, Andreas Grünwald
//All rights reserved.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.IO;
using System.Collections.ObjectModel;
using yaTDL.Properties;
using yaTDL.Core;

namespace yaTDL
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {


        static string newCrashReport_FileName
        {
            get
            {
#if DEBUG
                string directory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\yaTDL_Debug\CrashReports\";
#else
                string directory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\yaTDL\CrashReports\";
#endif
               
                if(!Directory.Exists(directory))
                { 
                    Directory.CreateDirectory(directory);
                }

                return (directory + DateTime.Now.ToString().Replace(":", "") + ".txt");
            }
        }



        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {

            string errorMessage = "An unhandled Exception occured at " + DateTime.Now.ToString() + "\n\n";

            errorMessage += "Source:\n";
            errorMessage += e.Exception.Source;
            errorMessage += "\n";
            errorMessage += "Message:\n";
            errorMessage += e.Exception.Message;
            errorMessage += "\n";

            errorMessage += "TargetSite:\n";
            errorMessage += e.Exception.TargetSite.Name;
            errorMessage += "\n";

            errorMessage += "StackTrace:\n";
            errorMessage += e.Exception.StackTrace;
            errorMessage += "\n";

            errorMessage += "InnerException:\n";
            errorMessage += e.Exception.InnerException;



            File.WriteAllText(newCrashReport_FileName, errorMessage);



            yaTDL.Core.UI.Dialogs.ShowMessageBox(errorMessage, "Unhandled Exception", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);

            this.MainWindow.Close();
            this.Shutdown();

        }


        private void Application_Exit(object sender, ExitEventArgs e)
        {
            ApplicationEvents.OnApplicationClosing();
            
            ApplicationEvents.Dispose();
        }

    }
}
